require 'rake'
require 'rake/clean'
require 'rake/rdoctask'
require 'rake/testtask'
require 'spec/rake/spectask'
require 'fileutils'
def __DIR__
  File.dirname(__FILE__)
end

include FileUtils

task :default => [:specs]
task :doc => [:rdoc]

Rake::RDocTask.new do |rdoc|
     files =['README', 'LICENSE', 'TODO', 'lib/**/*.rb']
     rdoc.rdoc_files.add(files)
     rdoc.main = "README" # page to start on
     rdoc.title = "Merb RestfulController Mixin Docs"
     # rdoc.template = __DIR__+"/tools/allison/allison.rb"
     rdoc.rdoc_dir = 'doc' # rdoc output folder
     rdoc.options << '--line-numbers' << '--inline-source'
end


desc 'Run unit tests'
Rake::TestTask.new('test_unit') do |t|
  t.libs << 'test'
  t.pattern = 'test/unit/*_test.rb'
  t.verbose = true
end

desc 'Run functional tests'
Rake::TestTask.new('test_functional') do |t|
  t.libs << 'test'
  t.pattern = 'test/functional/*_test.rb'
  t.verbose = true
end

desc 'Run all tests'
Rake::TestTask.new('test') do |t|
  t.libs << 'test'
  t.pattern = 'test/**/*_test.rb'
  t.verbose = true
end

desc "Run all specs"
Spec::Rake::SpecTask.new('specs') do |t|
  t.spec_opts = ["--format", "specdoc", "--colour"]
  t.libs = ['lib' ]
  t.spec_files = Dir['specs/**/*_spec.rb'].sort
end

desc "Run all specs output html"
Spec::Rake::SpecTask.new('specs_html') do |t|
  t.spec_opts = ["--format", "html"]
  t.libs = ['lib' ]
  t.spec_files = Dir['specs/**/*_spec.rb'].sort
end

desc "RCov"
Spec::Rake::SpecTask.new('rcov') do |t|
  t.spec_opts = ["--format", "specdoc", "--colour"]
  t.spec_files = Dir['specs/**/*_spec.rb'].sort
  t.libs = ['lib' ]
  t.rcov = true
end

STATS_DIRECTORIES = [
  %w(Code               lib/),
  %w(Unit\ tests        specs),
].collect { |name, dir| [ name, "./#{dir}" ] }.select { |name, dir| File.directory?(dir) }

desc "Report code statistics (KLOCs, etc) from the application"
task :stats do
  require __DIR__ + '/tools/code_statistics'
  #require 'extra/stats'
  verbose = true
  CodeStatistics.new(*STATS_DIRECTORIES).to_s
end

##############################################################################
# SVN
##############################################################################

desc "Add new files to subversion"
task :svn_add do
   system "svn status | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | xargs svn add"
end
